package com.company.JUC.AtomicDemo.demo1;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName Atomic
 * @Description TODO
 * @Author 11
 * @Date 2021/6/21 14:56
 * @Version 1.0
 */
public class Atomic {
    private int num1 = 0;
    private AtomicInteger num2 = new AtomicInteger(0);

    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0,
                Runtime.getRuntime().availableProcessors()*2,
                3, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(2),
//                new SynchronousQueue<Runnable>(),
                new ThreadPoolExecutor.DiscardPolicy());//拒绝策略  丢掉当前任务

        Atomic atomic = new Atomic();
        for (int i = 0; i <100 ; i++) {
            threadPoolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    atomic.task2();
                }
            });
        }

        for (int i = 0; i <100 ; i++) {
            threadPoolExecutor.execute(atomic::task1);
        }

        try {
            Thread.sleep(1000*2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(atomic.getNum1()+"        "+atomic.getNum2());

    }

    public int getNum1() {
        return num1;
    }

    public AtomicInteger getNum2() {
        return num2;
    }

    private void task1(){
        for (int i = 0; i <1000 ; i++) {
            this.num1++;
        }
    }
    private void task2(){
        for (int i = 0; i <1000 ; i++) {
            this.num2.incrementAndGet();
        }
    }

}
